package com.service.impl;

import com.service.MSGService;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Map;
@Service
public class MSGServiceImpl implements MSGService {
    @Override
    public boolean send(Map map, String phone) {
        if(StringUtils.isEmpty(phone)) return false;

        Config config = new Config()
                .setAccessKeyId("LTAI5tKAz2Y346MaxMvor4nB")
                .setAccessKeySecret("A8r7dy1QYcij8ZoJxDSu6VToiIFiQQ");

        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client = null;
        try{
            client = new Client(config);
            SendSmsRequest request = new SendSmsRequest();

            request.setSignName("阿里云短信测试");//签名名称
            request.setTemplateCode("SMS_154950909");//模版Code
            request.setPhoneNumbers(phone);
            request.setTemplateParam(JSONObject.toJSONString(map));
            SendSmsResponse response = client.sendSms(request);
            System.out.println("发送成功："+new Gson().toJson(response));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String checkCode(String code, HttpSession session) {
        System.out.println("进入MSGServiceImpl下的checkCode");
        String Rel_code = (String) session.getAttribute("code");
        if(code.equals(Rel_code)) {
            System.out.println("code:" + code + " Rel_code:" + Rel_code);
            return "Success";
        }
        else
            return "验证码错误";
    }
}
